package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.Product;
import cn.wolfcode.shop.api.domain.ProductSku;
import cn.wolfcode.shop.api.domain.SkuProperty;
import cn.wolfcode.shop.api.domain.SkuPropertyValue;
import cn.wolfcode.shop.api.service.IProductService;
import cn.wolfcode.shop.api.service.IProductSkuService;
import cn.wolfcode.shop.api.service.ISkuPropertyService;
import cn.wolfcode.shop.api.service.ISkuPropertyValueService;
import cn.wolfcode.shop.api.vo.CreateSkuVo;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import cn.wolfcode.shop.api.vo.ProductSkuSaveVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("productSku")
public class ProductSkuController {
    @Reference
    private IProductSkuService productSkuService;
    @Reference
    private IProductService productService;
    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private ISkuPropertyValueService skuPropertyValueService;

    /**
     * 控制访问产品sku页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public String productSku(@PathVariable("id") Long id, Model model) {
        Product product = productService.get(id);
        model.addAttribute("skuPropertyList", skuPropertyService.getSkuProperty(product.getCatalog().getId()));
        if (!productSkuService.isGenerated(id)) {
            model.addAttribute("product", product);
            return "property/sku_generate";
        }
        model.addAttribute("productId",id);
        model.addAttribute("productSkuList", productSkuService.selectSkuByProductId(product.getId()));
        return "product/sku_manage";
    }

    @RequestMapping(value = "reGenerate/{id}", method = RequestMethod.GET)
    public String reGenerateSku(@PathVariable("id") Long id, Model model) {
        Product product = productService.get(id);
        model.addAttribute("skuPropertyList", skuPropertyService.getSkuProperty(product.getCatalog().getId()));
        model.addAttribute("product", product);
        return "property/sku_generate";
    }


    /**
     * 根据sku属性获取sku属性值列表并显示
     */
    @RequestMapping(value = "skuPropertyValue/{id}", method = RequestMethod.GET)
    public String skuPropertyValue(@PathVariable("id") Long id, Model model) {
        List<SkuPropertyValue> skuPropertyValue = skuPropertyValueService.selectBySkuPropertyId(id);
        model.addAttribute("skuPropertyValue", skuPropertyValue);
        model.addAttribute("skuProperty", skuPropertyService.get(id));
        return "property/sku_property_value_table";
    }

    /**
     * 生成sku码
     *
     * @param model
     * @param createSkuVo
     * @return
     */
    @RequestMapping("generateSku")
    public String creatSKU(Model model, @RequestBody CreateSkuVo createSkuVo) {
        //获取sku
        List<ProductSku> productSkus = productSkuService.creatSKU(createSkuVo);
        //获取分类
        List<SkuProperty> skuPropertyList = createSkuVo.getSkuPropertyList();

        model.addAttribute("skuList", productSkus);
        model.addAttribute("skuPropertyList", skuPropertyList);
        return "property/sku_form";
    }

    /**
     * 保存sku码
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultVo save(ProductSkuSaveVo vo) {
        try {
            Long productId = vo.getProduct().getId();
            List<ProductSku> productSkuList = vo.getProductSkuList();
            productSkuService.saveSku(productId, productSkuList);
            return new JsonResultVo();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResultVo(false, e.getMessage(), null);
        }
    }

}
